# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='date'
pkgname="ruby-${_gemname}"
pkgver=3.2.2
pkgrel=4
pkgdesc='A subclass of Object includes Comparable module for handling dates'
arch=('loong64' 'x86_64')
url='https://github.com/ruby/date'
license=('BSD' 'RUBY')
depends=('ruby')
makedepends=('ruby-rake' 'ruby-rake-compiler')
options=('!emptydirs')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('5a640b550c9ea0c1fa24aa5d114ae9108fd568c8c38b20434168a92c886d66487065e0c6aedc7e55cbe59cc3338fd262ca601267ed0b55377c382fe0502e9604')
b2sums=('aedb4d5d43b1d7ac277bc6fd36651f36828be7a04f4eb75c45ea02bc56b3a403af9e8a26b23a9839c904ae455e6a02476fe83115d8c816f06168db6e0a788347')

prepare() {
  cd "${_gemname}-${pkgver}"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"
  local _platform="$(gem env platform | cut -d':' -f2)"
  local _extension_api_version="$(ruby -e 'puts Gem.extension_api_version')"

  install --verbose --directory --mode=0755 \
    "tmp_install_default/gemspec/specifications/gems/${_gemname}-${pkgver}" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}" \
    "tmp_install${_gemdir}/specifications/default"

  gem build "${_gemname}.gemspec"

  gem install \
    --local \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  gem install \
    --default \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install_default/gemspec" \
    --bindir "tmp_install_default/usr/bin" \
    "${_gemname}-${pkgver}.gem"

  mv --verbose "tmp_install${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" "tmp_install${_gemdir}/specifications/default/${_gemname}-${pkgver}.gemspec"
  mv --verbose "tmp_install${_gemdir}/gems/${_gemname}-${pkgver}/lib/${_gemname}.rb" "tmp_install/usr/lib/ruby/${_extension_api_version}/${_gemname}.rb"
  mv --verbose "tmp_install${_gemdir}/extensions/${_platform}/${_extension_api_version}/${_gemname}-${pkgver}/"* "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}/"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install${_gemdir}/cache/" \
    "tmp_install${_gemdir}/build_info/" \
    "tmp_install${_gemdir}/extensions/" \
    "tmp_install${_gemdir}/gems/" \
    "tmp_install${_gemdir}/plugins/" \
    "tmp_install${_gemdir}/specifications/${_gemname}-${pkgver}.gemspec" \
    "tmp_install${_gemdir}/doc/${_gemname}-${pkgver}/ri/ext/" \
    "tmp_install/usr/lib/ruby/${_extension_api_version}/${_platform}/"{gem.build_complete,gem_make.out,mkmf.log}
}

check() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake test
}

package() {
  cd "${_gemname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
